package org.sang.service;


import org.sang.bean.MbrCard;
import org.sang.bean.MbrCardExample;
import org.sang.common.SnowflakeIdGeneratorUtils;
import org.sang.common.UserUtils;
import org.sang.mapper.ext.MbrCardExtMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Description: 会员卡操作业务 .<br>
 *
 * @author jm      Created By 2018/4/4 下午1:41
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MbrCardService {


    @Autowired
    private MbrCardExtMapper mbrCardExtMapper;

    /**
     * Add mbr card int.
     *
     * @param mbrCard the mbr card
     * @return the int
     */
    public int addMbrCard(MbrCard mbrCard) {
        mbrCard.setId(SnowflakeIdGeneratorUtils.getId());
        mbrCard.setCreatedTime(new Date());
        mbrCard.setUpdateTime(new Date());
        mbrCard.setCreatedOperator(UserUtils.getCurrentUser().getUsername());
        mbrCard.setUpdateOperator(UserUtils.getCurrentUser().getUsername());

        return mbrCardExtMapper.insert(mbrCard);

    }


    /**
     * 修改.
     *
     * @param mbrCard the mbr card
     * @return int
     */
    public int updateMbrCard(MbrCard mbrCard) {
        mbrCard.setUpdateTime(new Date());

        mbrCard.setUpdateOperator(UserUtils.getCurrentUser().getUsername());
        MbrCardExample mbrCardExample = new MbrCardExample();
        mbrCardExample.createCriteria().andIdEqualTo(mbrCard.getId());
        return mbrCardExtMapper.updateByExampleSelective(mbrCard, mbrCardExample);

    }

    /**
     * 删除.
     *
     * @param id the id
     * @return the int
     */

    public int deleteMbrCard(String id) {
        MbrCardExample mbrCardExample = new MbrCardExample();
        mbrCardExample.createCriteria().andIdEqualTo(id);
        return mbrCardExtMapper.deleteByExample(mbrCardExample);

    }

    /**
     * 查找单个.
     *
     * @param id the id
     * @return the mbr card
     */
    public MbrCard queryMbrCard(String id) {

        MbrCardExample mbrCardExample = new MbrCardExample();
        mbrCardExample.createCriteria().andIdEqualTo(id);
        List<MbrCard> list = mbrCardExtMapper.selectByExample(mbrCardExample);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            return list.get(0);
        }


    }

    /**
     * 按照门店编号查找列表.
     *
     * @param mbrCard the mbr card
     * @return the list
     */
    public List<MbrCard> queryMbrCards(MbrCard mbrCard) {

        MbrCardExample mbrCardExample = new MbrCardExample();
        mbrCardExample.createCriteria().andStoreIdEqualTo(mbrCard.getStoreId());
        return mbrCardExtMapper.selectByExample(mbrCardExample);

    }


    /**
     * 按照门店号和卡号查找
     *
     * @param mbrCard the mbr card
     * @return mbr card
     */
    public MbrCard queryMbrCardByCardNumberAndStore(MbrCard mbrCard) {

        MbrCardExample mbrCardExample = new MbrCardExample();
        mbrCardExample.createCriteria().andStoreIdEqualTo(mbrCard.getStoreId())
            .andCardNoEqualTo(mbrCard.getCardNo());
        List<MbrCard> list = mbrCardExtMapper.selectByExample(mbrCardExample);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            return list.get(0);
        }

    }

    /**
     * 更新余额.
     *
     * @param mbrCard the mbr card
     */
    public void updateMbrCardBalance(MbrCard mbrCard) {
        mbrCard.setUpdateOperator(UserUtils.getCurrentUser().getUsername());
        mbrCard.setUpdateTime(new Date());
        mbrCardExtMapper.updateBalance(mbrCard);

    }
}
